import React from 'react'
import { BrowserRouter, Switch, Route, Redirect } from "react-router-dom";
import routerList from './routerList';
export default function Router() {
    return (
        <BrowserRouter>
            <RouterView routerList={routerList}></RouterView>
        </BrowserRouter>
    )
}



function RouterView(props) {
    const { routerList } = props;
    return (
        <Switch>
            {
                routerList.length > 0 && routerList.map((v, i) => {
                    return v.path ? <Route key={i} path={v.path} render={(props) => <BeforeEach {...v} {...props} />} /> : <Redirect exact key={i} {...v} />
                })
            }
        </Switch>
    )
}

function BeforeEach(props) {
    const Com = props.component;
    console.log(props.path);
    return (
        <>
            <Com {...props} />
            {
                localStorage.getItem("token") ? "" : <Redirect to="/login" />
            }
        </>
    )
}